// pages/optimal/optimal.js
const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        productlist: '',
        bottom_tap: false,
        checkpro: '',
        showdel: false,
        unpro: false,
        delBg:false,
        currentIndex:'',
        myinfo:''
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        app.func.onPageLoad(this, options);
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.optimal(0);
        this.myinfos()
    },

    optimal: function (offent) {
        app.func.getPromise('/cart/' + offent + '?length=10')
            .then(([code, res]) => {
                if (offent == 0) {
                    if (res.data.length == 0) {
                        this.setData({unpro: true});
                    } else {
                        this.setData({unpro: false});
                    }
                    this.setData({
                        productlist: this.dealData(res.data), bottom_tap: false, checkpro: '',
                    });
                } else if (res.data.length == 0) {
                    this.setData({bottom_tap: true});
                } else {
                    var tempArray = this.data.productlist;
                    tempArray = tempArray.concat(   this.dealData(res.data)  );
                    this.setData({productlist: tempArray});
                }
                wx.hideLoading();
            })
    },


    dealData: function (array) {
        for (let i = 0; i < array.length; i++) {
                array[i].check = false;
        }
        console.log(array);
        return array;
    },

    showDel:function(e){
       let index = e.currentTarget.dataset.index;
       let skuList = this.data.productlist;
       skuList[index].check = true;
       this.setData({ productlist:skuList, delBg:true, currentIndex:index });
    },
    hideDel:function(){
       let index = this.data.currentIndex;
       let skuList = this.data.productlist;
        skuList[index].check = false;
       this.setData({ delBg:false, productlist:skuList });
    },


    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.data.bottom_tap) {
            this.setData({bottom_tap: true});
        } else {
            wx.showLoading({title: '加载更多', mask: true});
            var productlist = this.data.productlist;
            this.optimal(productlist.length);
        }
    },

    //产品单删除
    delete_product: function (e) {
        var that = this;
        var id = e.currentTarget.dataset.id;
        console.log(id);
        wx.showModal({
            title: '移除收藏',
            content: '收藏一旦删除，可从商品详情重新添加',
            success: function (res) {
                if (res.confirm) {
                    app.func.getPromise('/delcart/' + id)
                        .then(([code, res]) => {
                            if (code == 200) {
                                that.setData({ delBg:false},()=>{
                                    that.optimal(0);
                                });
                            }
                        })
                } else {
                    console.log('用户点击取消')
                }

            }
        })
    },

    //批量删除
    choosedel: function () {
        this.setData({showdel: true});
    },
    canceldel: function () {
        this.setData({showdel: false, checkpro: ''});
    },
    checkboxChange: function (e) {
        var pro = e.detail.value;
        this.setData({checkpro: pro});
    },
    delCheck: function () {
        let checkpro = this.data.checkpro;
        if (checkpro.length == 0) {
            app.func.toastPromise('请选择商品');
            return;
        } else {
            app.func.wxPromise('showModal', {content: '确定删除所选商品'})
                .then((res) => {
                    console.log(checkpro)
                    console.log(res)
                    if (res.confirm) {
                        this.delSku(checkpro);
                    } else if (res.cancel) {
                        this.setData({showdel: false});
                    }
                })
        }
    },

    delSku: function (checkpro) {
        console.log(checkpro)
        app.func.postPromise('/batchdelchoose', {
            sku: checkpro
        }).then(([code, res]) => {
            console.log(res)
            if (code == 200) {
                wx.pageScrollTo({scrollTop: 0, duration: 0});
                app.func.toastPromise('删除成功');
                this.setData({
                    showdel: false
                }, this.optimal(0));
            } else {
                app.func.toastPromise(res.message);
            }
        })
    },
    myinfos(){
        app.func.getPromise('/my/info')
                .then(([code, res]) => {
                this.setData({
                        myinfo: res.message
                });
        })
    },

    addpro: function () {
        // wx.switchTab({
        //     url: '/pages/index/categorysku/categorysku'
        // })
        let extConfig = wx.getExtConfigSync? wx.getExtConfigSync(): {} ;
        if(extConfig.category = null || extConfig.category == 0) {
            wx.navigateTo({
                url: '/pages/index/categorysku/categorysku',
            })
        } else {
            wx.navigateTo({
                url: '/pages/index/newcategory/newcategory',
            })
        }
    },

    //商品详情
    productdetail: function (e) {
        var id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '/pages/index/productdetail/productdetail?id=' + id
        })
    },
    //货源说明
    product_info: function (e) {
        var id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '/pages/index/productdetail/product-description/product-description?id=' + id
        })
    },
    //购买
    apply: function (e) {
        var id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '/pages/cartshop/payif/payif?skuid=' + id
        })
    },


})
